package com.markbuikema.juliana32.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.markbuikema.juliana32.R;
import com.markbuikema.juliana32.model.teams.Team;
import com.markbuikema.juliana32.util.DataManager;
import com.markbuikema.juliana32.util.Util;

public class DrawerAdapter extends BaseAdapter {

	private String[] items;
	private Context context;
	private int selected;

	public DrawerAdapter(Context context) {
		this.context = context;
		items = context.getResources().getStringArray(R.array.drawer_items);
	}

	public void setSelected(int selected) {
		this.selected = selected;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return items.length + DataManager.getInstance().getTeams().size() + 1;
	}

	@Override
	public Object getItem(int position) {
		switch (ViewType.values()[getItemViewType(position)]) {
		case HEADER:
			return context.getResources().getString(R.string.drawer_teams_header);
		case MENU:
			return items[position];
		case TEAM:
			return DataManager.getInstance().getTeams().get(position - 1 - items.length);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getViewTypeCount() {
		return ViewType.values().length;
	}

	@Override
	public boolean isEnabled(int position) {
		return getItemViewType(position) != ViewType.HEADER.ordinal();
	}

	@Override
	public int getItemViewType(int position) {

		if (position < items.length) {
			return ViewType.MENU.ordinal();
		} else if (position == items.length) {
			return ViewType.HEADER.ordinal();
		} else {
			return ViewType.TEAM.ordinal();
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup vg) {
		ViewType type = ViewType.values()[getItemViewType(position)];

		if (convertView == null || ((ViewType) convertView.getTag()) != type) {
			convertView = LayoutInflater.from(context).inflate(type.getLayout(), null);
			convertView.setTag(type);
		}

		TextView text = ((TextView) convertView);
		Object item = getItem(position);

		switch (type) {
		case HEADER:
			text.setTypeface(Util.getRobotoLight(context), Typeface.BOLD);
			text.setText((String) item);
			break;
		case MENU:
			text.setTypeface(Util.getRobotoLight(context));
			text.setText((String) item);
			break;
		case TEAM:
			text.setTypeface(Util.getRobotoLight(context));
			text.setText(((Team) item).getName());
			break;
		}

		convertView.setBackgroundResource(selected == position ? R.color.list_item_blue : android.R.color.transparent);

		return convertView;
	}

	private enum ViewType {
		MENU(
				R.layout.drawer_item_menu),
		HEADER(
				R.layout.drawer_item_header),
		TEAM(
				R.layout.drawer_item_team);

		private int layout;

		private ViewType(int layout) {
			this.layout = layout;
		}

		public int getLayout() {
			return layout;
		}

	}
}
